<template>
  <el-dialog :title="title" :visible.sync="visible" width="600px" append-to-body :destroy-on-close="true">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="选项" prop="label">
        <el-input v-model="form.label"></el-input>
      </el-form-item>
      <el-form-item label="得分" prop="label">
        <el-input v-model="form.score"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm"> 保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postRequest } from '@/api/baseRequest'

export default {
  name: 'FormScoreModal',
  data() {
    return {
      visible: false,
      title: '',
      buttonLoading: false,
      form: {
        id: '',
        label: '',
        score: 0,
        formKey: ''
      }
    }
  },
  methods: {
    add() {
      this.visible = true
      this.reset()
    },
    update() {
      this.visible = true
    },
    reset() {
      this.form.id = ''
      this.form.label = ''
      this.form.score = 0
      this.resetForm('form')
    },
    submitForm() {
      const _this = this
      console.log(this.form)
      let url = this.form.id ? '/form/score/update' : '/form/score/add'
      postRequest(url, this.form).then((res) => {
        if (res.code === 200) {
          _this.$message.success('保存成功')
          _this.visible = false
          _this.$emit('ok')
        } else {
          _this.$message.error(res.msg)
        }
      })
    },
    cancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped></style>
